package demo.miner;

import static demo.miner.ClassEntity.ClassName.ARCHERY;
import static demo.miner.ClassEntity.ClassName.BARRACKS;
import static demo.miner.ClassEntity.ClassName.FOOTMAN;
import static demo.miner.ClassEntity.ClassName.PEASANT;
import static demo.miner.Team.BLUE;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import jtech.JTech;
import jtech.math.Vec2f;
import demo.miner.system.Configure;

public class GameMain extends JTech {

    public static void main(String[] args) {
        System.setProperty("sun.awt.enableExtraMouseButtons", "true");
        new GameMain().startGame();
    }
    
    private Game world = new Game();

    EntityUnit unit1, unit2, unit3;
    
    @Override
    public void init() {
        setFrameRate(60);
        setWindowMode(Game.GRID_SIZE*10, Game.GRID_SIZE*8);
        
        setMouseVisible(true);
        setRelativeMouseMode(false);
        
        Configure.define("data/unit", ClassUnit.class);
        Configure.define("data/building", ClassBuilding.class);
        
        world.setWindowWidth(Game.GRID_SIZE*10);
        world.setWindowHeight(Game.GRID_SIZE*8);
        world.createMap(10, 8);
        world.setTeamPlayer(BLUE);
        
        world.createEntity(BARRACKS, BLUE, "position", pos(32, 32));
        world.createEntity(BARRACKS, BLUE, "position", pos(128, 32));
        
//        world.createEntity(UNIT_PEON, TEAM_RED,    "x", 50,  "y", 50);
//        unit1 = world.createEntity(UNIT_PEASANT, TEAM_BLUE,   "x", 150, "y", 50);
        unit2 = world.createEntity(ARCHERY, BLUE,   "position", pos(200, 50));
//        world.createEntity(UNIT_PEON, TEAM_YELLOW, "x", 100, "y", 150);
        unit3 = world.createEntity(FOOTMAN, BLUE,   "position", pos(150, 150));
        unit3 = world.createEntity(PEASANT, BLUE,   "position", pos(182, 150));
        
    }

	private Vec2f pos(float x, float y) {
		return new Vec2f(x, y);
	}
    
    @Override
    public void shutdown() {
        // TODO Auto-generated method stub
        
    }
    
    public void update(float elapsedTime) {
    	
        if (isKeyPressed(KEY_ESC)) {
            stopGame();
            return;
        }
        
        if (isKeyPressed(KEY_F1)) {
        	Game.debug = !Game.debug;
        }

        
//        if (mouse.isReleased(MOUSE_BUTTON_LEFT)) {
//        	int mx = (mouse.getMouseX() / GRID_SIZE);
//            int my = (mouse.getMouseY() / GRID_SIZE);
//        	currentMapTerrainGrid[my][mx] = 1;
//        }
        
        world.action(this);
        
    	world.update(elapsedTime);
        
    }
    
    @Override
    public void draw(Graphics2D g2d) {
    	g2d.setBackground(Color.WHITE);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.clearRect(0, 0, getWindowSize().width, getWindowSize().height);

        
//        int[][] map = currentMapTerrainGrid;
//        for (int y = 0; y < map.length; y++) {
//        	for (int x = 0; x < map[y].length; x++) {
//        		int i = map[y][x];
//        		if (map[y][x] == 1) {
//        			g2d.setColor(new Color(255, 0, 0, 85));
//        			g2d.fillRect(x*GRID_SIZE, y*GRID_SIZE, GRID_SIZE, GRID_SIZE);
//        		}
//        		g2d.setColor(new Color(188, 196, 231, 128));
//        		g2d.setStroke(LINE_WIDTH_1);
//        		g2d.drawRect(x*GRID_SIZE, y*GRID_SIZE, GRID_SIZE, GRID_SIZE);
//        	}
//        }
        
//        
//        int mx = (mouse.getMouseX() / GRID_SIZE) * GRID_SIZE;
//        int my = (mouse.getMouseY() / GRID_SIZE) * GRID_SIZE;
//        
//        g2d.setColor(new Color(0, 255, 0, 128));
//		g2d.fillRect(mx, my, GRID_SIZE, GRID_SIZE);
        
        
		world.draw(getDrawer());
        
        
        String frames = String.format("Frames %s/%s", getFps(), getFrameRate());
        
        g2d.setColor(Color.BLACK);
        g2d.drawString(frames, 10, 10);
    }

}
